﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelMgr.Models;
using HotelMgr.ViewModels;

namespace HotelMgr.Controllers
{
    public class ShoppingCartController : Controller
    {
        HotelEntities db=new HotelEntities();
        //
        // GET: /ShoppingCart/

        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            //创建视图模型
            var viewModel = new ShoppingCartViewModel
                                {
                                    CartItems = cart.GetCartItems(),
                                    CartTotal = cart.GetTotal()
                                };
            //返回视图
            return View(viewModel);
        }
        //GET:/Hotel/AddToCart/5
        public ActionResult AddToCart(int id)
        {
            //
            var addedRoom = db.Rooms
                .Single(room => room.RoomID == id);
            //
            var cart = ShoppingCart.GetCart(this.HttpContext);
            cart.AddToCart(addedRoom);
            //
            return RedirectToAction("Index");
        }
        //

        //
        [HttpPost]
        public ActionResult RemoveFromCart(int id)
        {
            //
            var cart = ShoppingCart.GetCart(this.HttpContext);
            //
            int roomName = db.Carts
                .Single(item => item.RecordId == id).Room.RoomID;
            //
            int itemCount = cart.RemoveFormCart(id);
            //
            var results = new ShoppingCartRemoveViewModel
                              {
                                  Message = Server.HtmlEncode(roomName.ToString()) + "房间是否从订单里面移除。",
                                  CartTotal = cart.GetTotal(),
                                  CartCount = cart.GetCont(),
                                  ItemCount = itemCount,
                                  DeleteId = id
                              };
            return Json(results);

        }
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);
            ViewData["CartCount"] = cart.GetCont();
            return PartialView("CartSummary");
        }
    }
}
